import flask
from flask import Flask, render_template, jsonify, request
from flask_cors import CORS
from random import *
import json

app = Flask(__name__,
static_folder='./dist/static',
template_folder='./dist')
cors = CORS(app, resources={r"/*": {"origins": "*"}})

gm_db = []

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/api/get_all', methods=['GET'])
def get_all():
    return {'records': gm_db} 

@app.route('/api/add_sigle', methods=['POST'])
def add_sigle():
    req_data = request.get_json()
    # if not isinstance(req_data, dict):
    #     return jsonify({})

    req_data['index'] = len(gm_db) + 1
    gm_db.append(req_data)
    print(req_data)
    # print(gm_db)
    # print(type(req_data))
    return {'records': gm_db}



@app.route('/api/add_multi', methods=['POST'])
def add_multi():
    req_data = request.get_json()
    # print(req_data)

    for record in req_data['records']:
        record['index'] = len(gm_db) + 1
        # print(record)
        gm_db.append(record)


    return {'records': gm_db}

if __name__ == "__main__":
    # app.run(host='0.0.0.0', port='5000')
    app.run(port='5000')